package com.wan.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wan.service.BatchingInfoService;
import com.wan.service.VegetableInfoService;
import entity.ResultBean;
import entity.common.BatchingInfo;
import entity.common.VegetableInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import utils.TimeUtils;

import java.util.List;

@Slf4j
@Controller
@RequestMapping("/caipu")
public class VegetableController {

    @Autowired
    private VegetableInfoService vegetableInfoService;
    @Autowired
    private BatchingInfoService batchingInfoService;

    private static final long PAGE_SIZE = 10;
    /**
     * 文件上传
     * @param file
     * @return
     */
    @RequestMapping("/file/upload")
    @ResponseBody
    public ResultBean uploadFile(MultipartFile file){
        ResultBean resultBean =vegetableInfoService.uploadFile(file);
        return resultBean;
    }

    /**
     * 上传菜品
     * @param title
     * @param content
     * @param ids
     * @param imageFile
     * @param price
     * @param zkPrice
     * @param count
     * @param menuId
     * @return
     */
    @RequestMapping("/add/vegetable")
    @ResponseBody
    public ResultBean saveVegetable(String title,String content,String[] ids,String imageFile,Double price,Double zkPrice,Integer count,Integer menuId) {
        ResultBean resultBean = vegetableInfoService.saveVegetable(title,content,ids,imageFile,price,zkPrice,count,menuId);
        return resultBean;
    }

    @RequestMapping("/update/vegetable")
    @ResponseBody
    public ResultBean updateVegetable(Integer id,String title,String content,String[] ids,String imageFile,Double price,Double zkPrice,Integer count,Integer menuId) {
        ResultBean resultBean = vegetableInfoService.updateVegetable(id,title,content,ids,imageFile,price,zkPrice,count,menuId);
        return resultBean;
    }

    /**
     * 分页查询菜品列表
     * @return
     */
    @RequestMapping("/search/vegetable/list")
    @ResponseBody
    public ResultBean searchVegetableList(Long currPage,String title,Integer menuId) {
        IPage<VegetableInfo> page = new Page<>(currPage,PAGE_SIZE);
        QueryWrapper<VegetableInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",title);
        if (menuId != -1){
            queryWrapper.eq("menu_id",menuId);
        }
        queryWrapper.orderByDesc("update_time");
        IPage<VegetableInfo> vegetableInfoIPage = vegetableInfoService.page(page,queryWrapper);
        List<VegetableInfo> vegetableInfoList = vegetableInfoIPage.getRecords();
        Long count = vegetableInfoIPage.getTotal();
        long totalPage  = (count + PAGE_SIZE -1) / PAGE_SIZE;
        return new ResultBean(0,"成功",totalPage,vegetableInfoList);
    }

    /**
     * 上传配料
     * @param batchingInfo
     * @return
     */
    @RequestMapping("/add/batch")
    @ResponseBody
    public ResultBean saveBatch(BatchingInfo batchingInfo) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        batchingInfo.setUsername(authentication.getName());
        String batchUrl = batchingInfo.getImageUrl().substring(21);
        batchingInfo.setImageUrl(batchUrl);
        batchingInfo.setStatus(0);
        batchingInfo.setCreateTime(TimeUtils.GetNowDate("yyyy-MM-dd HH:mm:ss"));
        batchingInfo.setUpdateTime(TimeUtils.GetNowDate("yyyy-MM-dd HH:mm:ss"));
        Boolean b = batchingInfoService.save(batchingInfo);
        if (b) {
            return new ResultBean(200,"配料上传成功");
        }else {
            return new ResultBean(200,"配料上传失败");
        }
    }

    /**
     * 更新菜品状态
     * @param vegeId
     * @param status
     * @return
     */
    @RequestMapping("/vegetable/status/update")
    @ResponseBody
    public ResultBean updateStatus(Integer vegeId,Integer status){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        VegetableInfo vegetableInfo = new VegetableInfo();
        vegetableInfo.setUsername(authentication.getName());
        vegetableInfo.setId(vegeId);
        if (status == 0) {
            vegetableInfo.setStatus(1);
        }else {
            vegetableInfo.setStatus(0);
        }
        boolean b = vegetableInfoService.updateById(vegetableInfo);
        if (b) {
            return new ResultBean(200,"菜品状态更新成功");
        }else {
            return new ResultBean(500,"菜品状态更新失败");
        }
    }


    /**
     * 更新配料状态
     * @param batchId
     * @param status
     * @return
     */
    @RequestMapping("/batching/status/update")
    @ResponseBody
    public ResultBean updateBatchingStatus(Integer batchId,Integer status){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        BatchingInfo batchingInfo = new BatchingInfo();
        batchingInfo.setUsername(authentication.getName());
        batchingInfo.setId(batchId);
        if (status == 0) {
            batchingInfo.setStatus(1);
        }else {
            batchingInfo.setStatus(0);
        }
        boolean b = batchingInfoService.updateById(batchingInfo);
        if (b) {
            return new ResultBean(200,"配料状态更新成功");
        }else {
            return new ResultBean(500,"配料状态更新失败");
        }
    }

    @RequestMapping("/delete/vegetable")
    @ResponseBody
    public ResultBean deleteVegetable(Integer vegetableId){
        Boolean b = vegetableInfoService.removeById(vegetableId);
        if (b) {
            return new ResultBean(200,"删除成功");
        }else {
            return new ResultBean(500,"删除失败");
        }
    }
}
